package com.heo.sso.client.config;

import com.heo.sso.client.filter.LogoutFilter;
import com.heo.sso.client.filter.LoginFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author 刘康
 * @Date 2020-11-03 10:37
 * @Description
 * @Revision
 **/
@Configuration
public class SsoConfig {

    @Value("${sso.server.url}")
    private String ssoUrl;

    @Bean
    public FilterRegistrationBean<?> logoutFilter() {
        LogoutFilter logoutFilter = new LogoutFilter();
        FilterRegistrationBean<LogoutFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(logoutFilter);
        filterRegistrationBean.addUrlPatterns("/logout");
        filterRegistrationBean.setOrder(1);
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<?> loginFilter() {
        LoginFilter ssoFilter = new LoginFilter(ssoUrl);
        FilterRegistrationBean<LoginFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(ssoFilter);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setOrder(2);
        return filterRegistrationBean;
    }
}
